import React from 'react'
import axios from 'axios'
import { Button, Form, Input,message } from 'antd';
import {useNavigate} from 'react-router-dom'
export default function Login() {
    const navigate=useNavigate()
     
    const onFinish = (values) => {
        axios.post('/api/login',values).then((resp)=>{
            if (resp.data.code===200) {
                message.success("登录成功")
                localStorage.setItem('token',values.username)
                localStorage.setItem('role',resp.data.role)
                navigate('/zhut/home')
            }
        })
    };
    const onFinishFailed = (errorInfo) => {
        console.log('Failed:', errorInfo);
    };
    return (
        <div>
            <Form
                name="basic"
                labelCol={{
                    span: 8,
                }}
                wrapperCol={{
                    span: 16,
                }}
                style={{
                    maxWidth: 600,
                }}
                initialValues={{
                    remember: true,
                }}
                onFinish={onFinish}
                onFinishFailed={onFinishFailed}
                autoComplete="off"
            >
                <Form.Item
                    label="用户名(Username)"
                    name="username"
                    rules={[
                        {
                            required: true,
                            message: 'Please input your username!(请输入用户名!)',
                        },
                    ]}
                >
                    <Input />
                </Form.Item>

                <Form.Item
                    label="密码(Password)"
                    name="password"
                    rules={[
                        {
                            required: true,
                            message: 'Please input your password!(请输入密码!)',
                        },
                    ]}
                >
                    <Input.Password />
                </Form.Item>

                <Form.Item
                    wrapperCol={{
                        offset: 8,
                        span: 16,
                    }}
                >
                    <Button type="primary" htmlType="submit">
                        登录
                    </Button>
                </Form.Item>
            </Form>
        </div>
    )
}
